#include "def.h"
#include "2410addr.h"
#include "2410lib.h"
#include "2410slib.h"
#include "mmu.h"
#include "uart.h"
#include "timer.h"
#include "LCD.h"
#include "timer.h"

#include "sdi.h"
#include "Adcts.h"
#include "IIC.h"
#include "RTC.h"
#include "int.h"
#include "uart_test.h"
#include "uart0.h"
#include "uart1.h"
#include "lcd.h"
#include "led.h"
#include "buzzer.h"


U32	Console_Uart = 0;
U32	Console_Baud = 115200;


static void cal_cpu_bus_clk(void)
{
	U32 val;
	U8 m, p, s;
	
	val = rMPLLCON;
	m = (val>>12)&0xff;
	p = (val>>4)&0x3f;
	s = val&3;

	SYS_FCLK = ((m+8)*(FIN/100)*2)/((p+2)*(1<<s))*100;
	
	val = rCLKDIVN;
	m = (val>>1)&3;
	p = val&1;	
	val = rCAMDIVN;
	s = val>>8;
	
	switch (m) {
	case 0:
		SYS_HCLK = SYS_FCLK;
		break;
	case 1:
		SYS_HCLK = SYS_FCLK>>1;
		break;
	case 2:
		if(s&2)
			SYS_HCLK = SYS_FCLK>>3;
		else
			SYS_HCLK = SYS_FCLK>>2;
		break;
	case 3:
		if(s&1)
			SYS_HCLK = SYS_FCLK/6;
		else
			SYS_HCLK = SYS_FCLK/3;
		break;
	}
	
	if(p)
		SYS_PCLK = SYS_HCLK>>1;
	else
		SYS_PCLK = SYS_HCLK;

	rUPLLCON = (56<<12) | (2<<4) | 2;
}


static __inline int RtcAlmWake(void)
{	
	return ((rBCDHOUR==rALMHOUR)&&(rBCDMIN==rALMMIN));
}


struct {
	void (*fun)(void);
	char *tip;
}CmdTip[] = {
				{Test_SDI,"SD TEST"},
				{Test_Adc,"ADC TEST"},
				{Test_AdcTs,"TOUCH SCREEN TEST"},
				{Iic_Test,"EEPROM TEST"},
				{Rtc_Test,"RTC TEST"},
				{Test_Int,"INTERRUPT KEY TEST"},
				{BUZZER_PWM_Test,"BUZZER TEST"},
				{Uart_Test,"UART TEST"},
				{Lcd_Tft_320X240_Init,"LCD TEST"},
				{led,"LED TEST"},
				{0, 0}						
			};


int Main(U32 RstStat)
{
	int i;

	ChangeMPllValue(92,1,1);    //400MHZ
	SetClockDivider(2,1);		//fclk:hclk:pclk=1:4:8
	cal_cpu_bus_clk(); 
	
	rGPHCON=(rGPHCON&0x3ffff)|(0x0a<<18);
	
	rMISCCR=(rMISCCR&0x0f)|(0x02<<4);


    rMISCCR=(rMISCCR&0x0f)|(0x03<<4);
    
    rMISCCR=(rMISCCR&0x0f)|(0x04<<4);

	
	Port_Init();
	Isr_Init();

	Uart_Init(0, Console_Baud);
	Uart_Select(Console_Uart);	

#if 1
	MMU_Init();	//MMU should be reconfigured or turned off for the debugger, 

#else
	MMU_EnableICache();
#endif
	

	EnableModuleClock(CLOCK_ALL);	

//	LcdBackLightOff();
	Delay( 0 ) ;

	putch('\n');

//	PwrKeyChkInit();

	puts("************************************\n");
	puts("*                                  *\n");
	puts("*     YC2440 ADS TEST V1.01        *\n");
	puts("*     Http://www.yctek.com         *\n");
	puts("*                        2008-6-6  *\n");
	puts("************************************\n\n");

	printf("FCLK :%dMHz,HCLK:%dMHz,PCLK:%dMHz\n", SYS_FCLK/1000000,SYS_HCLK/1000000,SYS_PCLK/1000000);
	
	
	if(RstStat&1) {
		puts("Power on reset\n");
	}
	if(RstStat&4) {
		puts("Watchdog reset\n");
	}

	rMISCCR &= ~(0x3007);

	rGPBCON &= ~(3<<2);
	rGPBCON |= (1<<2);
	rGPBDAT &= ~(1<<1);
	rGPBUP &= ~(1<<1);	

	rGPBCON &= ~12;
	rGPBCON |= 4;	
	rGPBDAT |= 2;
	
	while(1)
	{
		U8 idx=0;
		puts("\nPlease select function : \n");	
		for(i=0; CmdTip[i].fun!=0; i++)
			printf("%d : %s\n", i, CmdTip[i].tip);
		puts("************************************\n\n");
		while((!idx))
		{
			idx=getkey();
		}
		
		idx -= '0';
		printf("%x\n",idx);
		if(idx<i)
		{
			(*CmdTip[idx].fun)();	
			
		}
	}
}